<script setup>
import { ref } from 'vue'
import { updateAdminPassword } from '@/api/admin'
import { ElMessage } from 'element-plus'

const passwordForm = ref({
  old_password: '',
  new_password: '',
  confirm_password: ''
})

const loading = ref(false)

const handleSubmit = async () => {
  if (passwordForm.value.new_password !== passwordForm.value.confirm_password) {
    ElMessage.error('两次输入的新密码不一致')
    return
  }

  loading.value = true
  try {
    await updateAdminPassword({
      old_password: passwordForm.value.old_password,
      new_password: passwordForm.value.new_password
    })
    ElMessage.success('密码修改成功')
    passwordForm.value = {
      old_password: '',
      new_password: '',
      confirm_password: ''
    }
  } catch (error) {
    ElMessage.error(error.response?.data?.message || '密码修改失败')
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="admin-password">
    <el-card class="password-card">
      <template #header>
        <h2>修改密码</h2>
      </template>
      
      <el-form 
        :model="passwordForm" 
        label-width="100px" 
        class="password-form"
        @submit.prevent="handleSubmit"
      >
        <el-form-item label="原密码" required>
          <el-input 
            v-model="passwordForm.old_password" 
            type="password" 
            placeholder="请输入原密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="新密码" required>
          <el-input 
            v-model="passwordForm.new_password" 
            type="password" 
            placeholder="请输入新密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="确认密码" required>
          <el-input 
            v-model="passwordForm.confirm_password" 
            type="password" 
            placeholder="请再次输入新密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            @click="handleSubmit"
            :loading="loading"
          >
            确认修改
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.admin-password {
  padding: 20px;
  max-width: 600px;
  margin: 0 auto;
}

.password-card {
  margin: 0 auto;
}

.password-form {
  max-width: 400px;
  margin: 0 auto;
}
</style>